package com.dlxj.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.dlxj.dao.BaseDAO;
import com.dlxj.entity.Bugtype;
import com.dlxj.entity.Circuitry;
import com.dlxj.entity.Debugtask;
import com.dlxj.entity.DebugtaskPole;
import com.dlxj.entity.DebugtaskUser;
import com.dlxj.entity.Patroltask;
import com.dlxj.entity.Pole;
import com.dlxj.entity.Receiptdebug;
import com.dlxj.entity.Receiptpatrol;
import com.dlxj.entity.User;
import com.dlxj.service.DeBugTaskService;
import com.dlxj.util.ConstClass;
import com.dlxj.util.PageBean;
import com.dlxj.util.excel.ExportExcel;
import com.dlxj.util.excel.ExportExcelBean;

public class DeBugTaskServiceImpl implements DeBugTaskService {

	BaseDAO baseDAO;

	ExportExcelBean exportExcelBean;

	@Override
	public int addDeBugTask(Debugtask debugtask, String dps) {
		if (baseDAO.findById(Debugtask.class, debugtask.getId()) != null) {
			return ConstClass.ADD_FAILURE;
		}
		debugtask.setCreateUser(ConstClass.getCurrentUser().getId());
		debugtask.setPrincipal(ConstClass.getCurrentUser().getId());
		debugtask.setState(ConstClass.TASK_STATE_WAIT);
		debugtask.setTime(new Date());
		String arr[] = dps.split(",");
		for (String tmp : arr) {
			String data[] = tmp.split(" - ");
			if (data.length <= 1) {
				return ConstClass.ADD_FAILURE;
			}
			String str = data[0];
			String pid = str.substring(str.indexOf(":") + 1, str.length());

			str = data[1];
			String ptid = str.substring(str.indexOf(":") + 1, str.length());
			DebugtaskPole debugtaskPole = new DebugtaskPole();
			debugtaskPole.setDid(debugtask.getId());
			debugtaskPole.setPid(pid);
			debugtaskPole.setPtid(ptid);
			baseDAO.save(debugtaskPole);
		}
		baseDAO.save(debugtask);

		return ConstClass.ADD_SUCCESS;
	}

	public BaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(BaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	@Override
	public List<Debugtask> findWait() {
		List<Debugtask> debugtasks = baseDAO.findByHql(
				"from Debugtask where state =?",
				new Object[] { ConstClass.TASK_STATE_WAIT });
		for (Debugtask debugtask : debugtasks) {
			List<Receiptpatrol> receiptpatrols = baseDAO
					.findByHql(
							"from Receiptpatrol where ptid in (select ptid from DebugtaskPole where did = ?) and pid in (select pid from DebugtaskPole where did = ?)",
							new Object[] { debugtask.getId(), debugtask.getId() });
			for (Receiptpatrol receiptpatrol : receiptpatrols) {
				Pole pole = (Pole) baseDAO.findById(Pole.class,
						receiptpatrol.getPid());
				receiptpatrol.setPole(pole);
				User u = (User) baseDAO.findById(User.class,
						receiptpatrol.getUid());
				receiptpatrol.setUserName(u.getName());
				Patroltask patroltask = (Patroltask) baseDAO.findById(
						Patroltask.class, receiptpatrol.getPtid());
				Circuitry circuitry = (Circuitry) baseDAO.findById(
						Circuitry.class, patroltask.getCid());
				receiptpatrol.setRouteName(circuitry.getName());
				Bugtype bt = (Bugtype) baseDAO.findById(Bugtype.class,
						receiptpatrol.getBid());
				receiptpatrol.setTypeName(bt.getName());
			}
			debugtask.setReceiptpatrols(receiptpatrols);

		}
		return debugtasks;
	}

	@Override
	public int assignUser(String did, String uids) {
		String[] ids = null;
		if (uids != null && uids.length() > 0) {
			ids = uids.split(",");
		} else {
			return ConstClass.ADD_FAILURE;
		}
		for (String uid : ids) {
			DebugtaskUser dbtu = new DebugtaskUser();
			dbtu.setDid(did);
			dbtu.setUid(Integer.valueOf(uid));
			baseDAO.save(dbtu);
		}
		Debugtask debugtask = (Debugtask) baseDAO
				.findById(Debugtask.class, did);
		debugtask.setState(ConstClass.TASK_STATE_ASSIGNED);
		baseDAO.update(debugtask);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public List<Debugtask> findWaitByUid() {
		List<Debugtask> debugtasks = baseDAO
				.findByHql(
						"from Debugtask where state =? and id in(select did from DebugtaskUser where uid =?)",
						new Object[] { ConstClass.TASK_STATE_ASSIGNED,
								ConstClass.getCurrentUser().getId() });
		for (Debugtask debugtask : debugtasks) {
			List<Receiptpatrol> receiptpatrols = baseDAO
					.findByHql(
							"from Receiptpatrol where ptid in (select ptid from DebugtaskPole where did = ?) and pid in (select pid from DebugtaskPole where did = ?)",
							new Object[] { debugtask.getId(), debugtask.getId() });
			for (Receiptpatrol receiptpatrol : receiptpatrols) {
				Pole pole = (Pole) baseDAO.findById(Pole.class,
						receiptpatrol.getPid());
				receiptpatrol.setPole(pole);
				User u = (User) baseDAO.findById(User.class,
						receiptpatrol.getUid());
				receiptpatrol.setUserName(u.getName());
				Patroltask patroltask = (Patroltask) baseDAO.findById(
						Patroltask.class, receiptpatrol.getPtid());
				Circuitry circuitry = (Circuitry) baseDAO.findById(
						Circuitry.class, patroltask.getCid());
				receiptpatrol.setRouteName(circuitry.getName());
				Bugtype bt = (Bugtype) baseDAO.findById(Bugtype.class,
						receiptpatrol.getBid());
				receiptpatrol.setTypeName(bt.getName());
			}
			debugtask.setReceiptpatrols(receiptpatrols);
		}
		return debugtasks;
	}

	@Override
	public int executeTask(String id) {
		Debugtask debugtask = (Debugtask) baseDAO.findById(Debugtask.class, id);
		debugtask.setState(ConstClass.TASK_STATE_EXECUTING);
		baseDAO.update(debugtask);
		return ConstClass.UPDATE_SUCCESS;
	}

	@Override
	public List<Debugtask> findExecuting() {
		List<Debugtask> debugtasks = baseDAO
				.findByHql(
						"from Debugtask where state =? and id in(select did from DebugtaskUser where uid =?)",
						new Object[] { ConstClass.TASK_STATE_EXECUTING,
								ConstClass.getCurrentUser().getId() });
		for (Debugtask debugtask : debugtasks) {
			List<Receiptpatrol> receiptpatrols = baseDAO
					.findByHql(
							"from Receiptpatrol where ptid in (select ptid from DebugtaskPole where did = ?) and pid in (select pid from DebugtaskPole where did = ?)",
							new Object[] { debugtask.getId(), debugtask.getId() });
			for (Receiptpatrol receiptpatrol : receiptpatrols) {
				List<Receiptdebug> receiptdebugs = baseDAO.findByHql(
						"from Receiptdebug where did= ? ",
						new Object[] { debugtask.getId() });
				if (receiptdebugs.size() > 0) {
					debugtask.setReceiptdebug(receiptdebugs.get(0));
				}

				Pole pole = (Pole) baseDAO.findById(Pole.class,
						receiptpatrol.getPid());
				receiptpatrol.setPole(pole);
				User u = (User) baseDAO.findById(User.class,
						receiptpatrol.getUid());
				receiptpatrol.setUserName(u.getName());
				Patroltask patroltask = (Patroltask) baseDAO.findById(
						Patroltask.class, receiptpatrol.getPtid());
				Circuitry circuitry = (Circuitry) baseDAO.findById(
						Circuitry.class, patroltask.getCid());
				receiptpatrol.setRouteName(circuitry.getName());
				Bugtype bt = (Bugtype) baseDAO.findById(Bugtype.class,
						receiptpatrol.getBid());
				receiptpatrol.setTypeName(bt.getName());
			}
			debugtask.setReceiptpatrols(receiptpatrols);
		}
		return debugtasks;
	}

	@Override
	public List<Debugtask> findAuditingMissPass() {
		List<Debugtask> debugtasks = baseDAO
				.findByHql(
						"from Debugtask where state =? and id in(select did from Receiptdebug where state = ?)",
						new Object[] { ConstClass.TASK_STATE_FINISH,
								ConstClass.TASK_AUDITING_MISSPASS });
		for (Debugtask debugtask : debugtasks) {
			List<Receiptpatrol> receiptpatrols = baseDAO
					.findByHql(
							"from Receiptpatrol where ptid in (select ptid from DebugtaskPole where did = ?) and pid in (select pid from DebugtaskPole where did = ?)",
							new Object[] { debugtask.getId(), debugtask.getId() });
			List<Receiptdebug> receiptdebugs = baseDAO.findByHql(
					"from Receiptdebug where did= ? ",
					new Object[] { debugtask.getId() });
			debugtask.setReceiptdebug(receiptdebugs.get(0));
			for (Receiptpatrol receiptpatrol : receiptpatrols) {
				Pole pole = (Pole) baseDAO.findById(Pole.class,
						receiptpatrol.getPid());
				receiptpatrol.setPole(pole);
				User u = (User) baseDAO.findById(User.class,
						receiptpatrol.getUid());
				receiptpatrol.setUserName(u.getName());
				Patroltask patroltask = (Patroltask) baseDAO.findById(
						Patroltask.class, receiptpatrol.getPtid());
				Circuitry circuitry = (Circuitry) baseDAO.findById(
						Circuitry.class, patroltask.getCid());
				receiptpatrol.setRouteName(circuitry.getName());
				Bugtype bt = (Bugtype) baseDAO.findById(Bugtype.class,
						receiptpatrol.getBid());
				receiptpatrol.setTypeName(bt.getName());
			}
			debugtask.setReceiptpatrols(receiptpatrols);

		}
		return debugtasks;
	}

	@Override
	public PageBean statistics(Debugtask debugtask,
			DebugtaskUser debugtaskUser, Date sd, Date ed, int pageIndex,
			int pageSize) {
		String hql = "from Debugtask where 1=1 ";
		List<Object> paramList = new ArrayList<Object>();
		if (debugtask.getCreateUser() != 0) {
			hql += " and createUser = ?";
			paramList.add(debugtask.getCreateUser());
		}
		if (!"".equals(debugtask.getType())) {
			hql += " and type = ?";
			paramList.add(debugtask.getType());
		}
		if (sd != null) {
			hql += " and time >=?";
			paramList.add(sd);
		}
		if (ed != null) {
			hql += " and time <=?";
			paramList.add(ed);
		}

		if (debugtaskUser.getUid() != 0) {
			hql += " and id in (select did from DebugtaskUser where uid=? )";
			paramList.add(debugtaskUser.getUid());
		}
		hql += " order by time desc";
		PageBean bean = baseDAO.findPageBeanHQL(hql, pageIndex, pageSize,
				paramList.toArray());
		List<Debugtask> rows = bean.getRows();
		for (Debugtask task : rows) {
			List<Receiptdebug> bugs = baseDAO.findByHql(
					"from Receiptdebug where did = ?",
					new Object[] { task.getId() });
			if (bugs.size() > 0) {
				task.setReceiptdebug(bugs.get(0));
			}
			User user = (User) baseDAO.findById(User.class,
					task.getCreateUser());
			task.setUserName(user.getName());
		}
		bean.setRows(rows);
		return bean;
	}

	@Override
	public String export(Debugtask debugtask, DebugtaskUser debugtaskUser,
			Date sd, Date ed) {
		PageBean bean = this.statistics(debugtask, debugtaskUser, sd, ed, 1,
				999999);
		if (bean.getTotal() == 0) {
			return "";
		}
		String fileName = "";
		try {
			exportExcelBean.setDataSource(bean.getRows());
			exportExcelBean.setTitle("消缺记录统计表");
			ExportExcel excel = new ExportExcel(exportExcelBean);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			fileName = sdf.format(new Date());
			excel.export(ServletActionContext.getRequest().getRealPath("")
					+ "\\xls\\" + fileName + ".xls");
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		return fileName;

	}

	public ExportExcelBean getExportExcelBean() {
		return exportExcelBean;
	}

	public void setExportExcelBean(ExportExcelBean exportExcelBean) {
		this.exportExcelBean = exportExcelBean;
	}

}
